<?php
namespace wechat\controllers;

use Yii;

use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;


use yii\helpers\Url;
/**
 * Site controller
 */
class CommunityController extends \base\BaseController
{
	public $layout = false;
	
	
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [];
    }



    public function actionIndex()
    {

        return $this->render('index');
    }

    public function actionDetails($id)
    {

        return $this->render('details', ['id'=> 'id']);
    }
	
    public function actionLists()
    {

        return $this->render('lists');
    }	
	
    public function actionPublish()
    {

        $options = array(
			'token'=>'tokenaccesskey', //填写你设定的key
			'encodingaeskey'=>'encodingaeskey', //填写加密用的EncodingAESKey
			'appid'=>'wx5f78741a981ddad1', //填写高级调用功能的app id
			'appsecret'=>'06b91c13fd6b5b0c88db07d4ee4378a3' //填写高级调用功能的密钥
		);
		
	    $weObj = new \common\components\Wechat($options);
        // 注意 URL 一定要动态获取，不能 hardcode.
		$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
		$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
		$signPackage = $weObj->getJsSign($url);
        return $this->render('publish', ['signPackage'=> $signPackage]);
    }	
	
	
		/**
	 * 发贴
	 */
	/**
	 * 通用上传接口 包括百度编辑器上传接口
	 * @param NULL
	 * @author xiong
	 */
	public function actionUpload(){
		//error_reporting( E_ERROR | E_WARNING );
		
		$config = array(
		    "savePath" => "../uploads/community",
			"maxSize" => isset($_POST['maxSize']) ? intval($_POST['maxSize']) : 20000,                   //允许的文件最大尺寸，单位KB
			"allowFiles" => isset($_POST['fileTypeExts']) ? explode(';', str_replace('*','', trim($_POST['fileTypeExts']))) : array( ".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp" ),  //允许的文件格式
			"thumbWidth" => 600,
		);

		$up = new \common\components\Uploader( "upfile" , $config );

		$info = $up->getFileInfo();
		if ($info['state'] == "SUCCESS") {
			$image = new \common\components\Ximage();
			$thumbimg = $info['url'].'_thumb_120'.strtolower(strrchr($info['url'], '.'));
			$fileurl = $image->thumb($info['url'], $thumbimg, 120, 0);
			$info['url'] = file_exists($fileurl) ? $fileurl : $info['url'];
			
		}else{
			$info['error'] = '';
		}
		echo json_encode($info);
	
		exit();
	}
}
